package io.github.quickmsg.core.protocol;

import io.github.quickmsg.common.channel.MqttChannel;
import io.github.quickmsg.common.context.ReceiveContext;
import io.github.quickmsg.common.log.LogEvent;
import io.github.quickmsg.common.log.LogManager;
import io.github.quickmsg.common.log.LogStatus;
import io.github.quickmsg.common.message.mqtt.CloseMessage;
import io.github.quickmsg.common.metric.CounterType;
import io.github.quickmsg.common.protocol.Protocol;
import io.github.quickmsg.common.utils.JacksonUtil;
import reactor.util.context.ContextView;


/**
 * @author luxurong
 */
public class CloseProtocol implements Protocol<CloseMessage> {

    @Override
    public void parseProtocol(CloseMessage message, MqttChannel mqttChannel, ContextView contextView) {
        ReceiveContext<?> receiveContext =  contextView.get(ReceiveContext.class);
        LogManager logManager = receiveContext.getLogManager();
        receiveContext.getMetricManager().getMetricRegistry().getMetricCounter(CounterType.CONNECT).decrement();
        receiveContext.getMetricManager().getMetricRegistry().getMetricCounter(CounterType.CLOSE_EVENT).increment();
        logManager.printInfo(mqttChannel, LogEvent.CLOSE, LogStatus.SUCCESS, JacksonUtil.bean2Json(message));

    }

    @Override
    public Class<CloseMessage> getClassType() {
        return CloseMessage.class;
    }
}
